// pages/luoAn/common/loginIndex.js
const app = getApp()
const CommonHttp = app.CommonHttp()

const tabsArray = [
  {
    code: 'login',
    name: '绑定已有账号'
  },
  {
    code: 'regist',
    name: '注册新账号'
  },
  
]

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabsArray,
    userInfo: {},
    currentTab: 0,


    loginUserName: null,
    loginPassword: null,
    loginCanSubmit: false,



    registCheckcodeEnable: false,
    registPhone: null,
    registCheckcode: null,
    registTimerCount: '获取验证码',
    registCansubmit: true,
    registPassword: null,
    registRepeatPassword: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const event = this.getOpenerEventChannel()
    event&&event.on('dataSource',userInfo=>this.setData({userInfo}))
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var index = e.target.dataset.current;
      that.setData({
        currentTab: e.target.dataset.current,
        currentIndex: index
      })
    }
  },
  swiperTab: function (e) {
    var index = e.detail.current;
    this.setData({
      currentTab: index,
      currentIndex: index
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  popMethod: function (e) {
    getApp().popMethod()
  },
  serviceDetail: function (e) {
    wx.navigateTo({
      url: '/pages/main/my/about/serviceProtocol',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  submitRegsit: async function () {
    const {userInfo,registPhone,registCheckcode,registPassword} = this.data;
    const param = {
      ...userInfo,
      // mobile:registPhone,
      // capatha:registCheckcode,
      // password:registPassword
    }
    const event = this.getOpenerEventChannel()
    const res = await CommonHttp.asyncRegistWithUnionId(param)
    if(res.status==200){
      event&&event.emit&&event.emit('callback',res.result)
    }
  },

  // 验证码输入
  registCheckcodeHandler: function (e) {
    let {
      registPhone,
      registRepeatPassword,
      registPassword,
      registCheckcode,
    } = this.data;
    registCheckcode = e.detail.value
    const phoneCheck = registPhone && registPhone.length == 11;
    const checkcodeCheck = registCheckcode >= 4;
    const passwordCheck = registPassword && registPassword.length > 0;
    const repeatPasswordChecked = registRepeatPassword && registRepeatPassword.length > 0;
    const sameChceked = registRepeatPassword == registPassword

    const isCanSubmit = phoneCheck && checkcodeCheck && passwordCheck && repeatPasswordChecked && sameChceked
    this.setData({
      registCheckcode: registCheckcode,
      registCansubmit: isCanSubmit
    })
  },
  registPhoneHandler: function (e) {
    let {
      registPhone,
      registRepeatPassword,
      registPassword,
      registCheckcode,
    } = this.data;
    registPhone = e.detail.value
    const phoneCheck = registPhone && registPhone.length == 11;
    const checkcodeCheck = registCheckcode >= 4;
    const passwordCheck = registPassword && registPassword.length > 0;
    const repeatPasswordChecked = registRepeatPassword && registRepeatPassword.length > 0;
    const sameChceked = registRepeatPassword == registPassword

    const isCanSubmit = phoneCheck && checkcodeCheck && passwordCheck && repeatPasswordChecked && sameChceked
    this.setData({
      registPhone: registPhone,
      registCansubmit: isCanSubmit,
      registCheckcodeEnable: phoneCheck
    })
  },
  passwordHandler: function (e) {
    let {
      registPhone,
      registRepeatPassword,
      registPassword,
      registCheckcode,
    } = this.data;
    registPassword = e.detail.value
    const phoneCheck = registPhone && registPhone.length == 11;
    const checkcodeCheck = registCheckcode >= 4;
    const passwordCheck = registPassword && registPassword.length > 0;
    const repeatPasswordChecked = registRepeatPassword && registRepeatPassword.length > 0;
    const sameChceked = registRepeatPassword == registPassword

    const isCanSubmit = phoneCheck && checkcodeCheck && passwordCheck && repeatPasswordChecked && sameChceked
    this.setData({
      registPassword: registPassword,
      registCansubmit: isCanSubmit
    })
  },
  repeatPasswordHandler: function (e) {
    let {
      registPhone,
      registRepeatPassword,
      registPassword,
      registCheckcode,
    } = this.data;
    registRepeatPassword = e.detail.value
    const phoneCheck = registPhone && registPhone.length == 11;
    const checkcodeCheck = registCheckcode >= 4;
    const passwordCheck = registPassword && registPassword.length > 0;
    const repeatPasswordChecked = registRepeatPassword && registRepeatPassword.length > 0;
    const sameChceked = registRepeatPassword == registPassword

    const isCanSubmit = phoneCheck && checkcodeCheck && passwordCheck && repeatPasswordChecked && sameChceked
    this.setData({
      registRepeatPassword: registRepeatPassword,
      registCansubmit: isCanSubmit
    })
  },


  loginSubmit: async function () {
    const {userInfo,loginUserName,loginPassword} = this.data;
    const param = {
      ...userInfo,
      username:loginUserName,
      password:loginPassword
    }
    const event = this.getOpenerEventChannel()
    const res = await CommonHttp.asyncBindAccountAndUnionId(param);
    if(res.status==200){
      event&&event.emit&&event.emit('callback',res.result)
    }
  },
  loginUserNameHandler: function (e) {
    let {
      loginUserName,
      loginPassword
    } = this.data
    loginUserName = e.detail.value;
    const userNameChecked = loginUserName && loginUserName.length > 0;
    const passwordCheck = loginPassword && loginPassword.length > 0;
    const loginCanSubmit = userNameChecked && passwordCheck
     
    this.setData({
      loginUserName,
      loginCanSubmit,
    })
  },
  loginPasswordHandler: function (e) {
    let {
      loginUserName,
      loginPassword
    } = this.data
    loginPassword = e.detail.value;
    const userNameChecked = loginUserName && loginUserName.length > 0;
    const passwordCheck = loginPassword && loginPassword.length > 0;
    const loginCanSubmit = userNameChecked && passwordCheck
     
    this.setData({
      loginPassword,
      loginCanSubmit,
    })
  }
})